package com.yd.obtainhashsign

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    companion object {
        const val Requst_Code = 100
        const val ResultKey = "Result_Key"

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        et_package_name.setText("")
        btn_get.setOnClickListener {
            val packageName = et_package_name.text.trim().toString()
            if (packageName.isNotEmpty()) {
                obtainResult(packageName)
            }
        }
        btnSelectPackage.setOnClickListener {
            startActivityForResult(
                Intent(this@MainActivity, SelectPackageActivity::class.java),
                Requst_Code
            )
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            Requst_Code -> {
                val result = data?.extras?.get(ResultKey) as String?
                handleResult(result)
            }
        }
    }

    private fun handleResult(packageName: String?) {
        packageName?.let {
            et_package_name.setText(it)
            obtainResult(it)
        }

    }

    private fun obtainResult(packageName: String) {
        val sha = AppSigningUtil.getFaceBookHash(this, packageName)
        val md5 = AppSigningUtil.getSingInfo(this, packageName, AppSigningUtil.MD5)
        val sha1 = AppSigningUtil.getSingInfo(this, packageName, AppSigningUtil.SHA1)
        val sha256 = AppSigningUtil.getSingInfo(this, packageName, AppSigningUtil.SHA256)
        tvShaResult.text = sha
        tvMd5Result.text = md5
        tvSha1Result.text = sha1
        tvSha256Result.text = sha256
    }


}
